#include "AccountList.h"
#include <commctrl.h>
#include <WindowsX.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;
AccountList::AccountList(string filename)
{
	char path[100]={0};
	GetModuleFileName(NULL,path,99);
	size_t len=strlen(path);
	for (size_t i=len-1;i>=0;i--)
	{
		if (path[i]=='\\' && i<len-1)
		{
			path[i+1]='\0';
			break;
		}
	}
	strcat_s(path,99,filename.c_str());
	const char *fileName=path;
	HANDLE hFile=CreateFile(fileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
	if (hFile==INVALID_HANDLE_VALUE)
	{
		printf("No AccountList.txt\n");
		return ;
	}
	BOOL ret=FALSE;
	char text[4096]={0};
	int length=0;
	ret=ReadFile(hFile,text,(DWORD)4096,(DWORD*)&length,NULL);
	string textstr=text;
	LoadAccountInfoFromString(textstr);
}

void AccountList::LoadAccountInfoFromString(string str)
{
	int first=0;
	int end=0;
	int flag=0;
	for (int i=0;i<str.length();i++)
	{
		if (str[i]==';')
		{
			end=i;
			if (flag==0)
			{
				vAccount.push_back(str.substr(first,end-first));
				flag++;
			}
			else if (flag==1)
			{
				vParentAccount.push_back(str.substr(first,end-first));
				flag++;
			}
			else if (flag==2)
			{
				vPassword.push_back(str.substr(first,end-first));
				flag++;
			}
			else if (flag==3)
			{
				vHost.push_back(str.substr(first,end-first));
				flag++;
			}
			else if (flag==4)
			{
				vServerId.push_back(str.substr(first,end-first));
				flag++;
			}
			first=i+1;
		}
		if ((int)str[i]==13)
		{
			first=i+2;
			flag=0;
			i++;
		}
	}
	for (int i=0;i<vAccount.size();i++)
	{
		account_id_map[vAccount[i]]=i;
	}
}

void AccountList::InsertAccountInfoToTreeView(HWND hWnd)
{
	HWND treeview=GetDlgItem(hWnd,IDC_TREE1);
	HTREEITEM hItem=NULL;
	TVITEM tvi;
	tvi.mask=TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIS_SELECTED | TVIS_EXPANDED;
	tvi.pszText="";
	tvi.cchTextMax=MAX_PATH;
	tvi.iImage =0;
	tvi.iSelectedImage = 1;
	TVINSERTSTRUCT tvis;
	tvis.hParent=TVI_ROOT;
	tvis.hInsertAfter=TVI_LAST;
	tvis.item=tvi;
	map<string,HTREEITEM> parentAccount_treeItem_map;
	for (int i=0;i<vParentAccount.size();i++)
	{
		if (i>0 && vParentAccount[i]==vParentAccount[i-1])
			continue;
		tvis.item.pszText=(LPSTR)vParentAccount[i].c_str();
		HTREEITEM h=TreeView_InsertItem(treeview,&tvis);
		parentAccount_treeItem_map[vParentAccount[i]]=h;
	}
	for (int i=0;i<vAccount.size();i++)
	{
		tvis.item.pszText=(LPSTR)vAccount[i].c_str();
		tvis.hParent=parentAccount_treeItem_map[vParentAccount[i]];
		TreeView_InsertItem(treeview,&tvis);
	}

}

string AccountList::GetSelectedAccount(HWND hWnd)
{
	HWND tv=GetDlgItem(hWnd,IDC_TREE1);
	TVITEM tvi;
	tvi.mask=TVIF_TEXT;
	char text[20]="";
	tvi.pszText=text;
	tvi.cchTextMax=20;
	tvi.hItem=TreeView_GetSelection(tv);
	TreeView_GetItem(tv,&tvi);;
	SetFocus(tv);
	map<string,int>::iterator it;
	it=account_id_map.find(text);
	int ret=-1;
	if (it!=account_id_map.end())
	{
		ret=it->second;
		selectedIndex=ret;
		return it->first;
	}
	return "";
}

string AccountList::GetSelectedParentAccount()
{
	return vParentAccount[selectedIndex];
}
string AccountList::GetSelectedPassword()
{
	return vPassword[selectedIndex];
}

string AccountList::GetSelectedHost()
{
	return vHost[selectedIndex];
}

string AccountList::GetSelectedServerId()
{
	return vServerId[selectedIndex];
}

